#include "libAcoustX/ocean/OceanDataset.h"

ax::ocean::Dataset::Dataset()
{
}

ax::ocean::Dataset::~Dataset()
{
}

void ax::ocean::Dataset::setSspDB(std::shared_ptr<darr::DimsArray<float>> ssp_db)
{
	if (ssp_db &&
		ssp_db->hasDimension(DIM_DEPTH_NAME) &&
		ssp_db->hasDimension(DIM_LAT_NAME) &&
		ssp_db->hasDimension(DIM_LON_NAME))
	{
		mSspDB = ssp_db;
	}
}

std::shared_ptr<darr::DimsArray<float>> ax::ocean::Dataset::getSspDB() const
{
	return mSspDB;
}

void ax::ocean::Dataset::setSedimentDB(std::shared_ptr<darr::DimsArray<short>> sed_db)
{
	if (sed_db &&
		sed_db->hasDimension(DIM_LAT_NAME) &&
		sed_db->hasDimension(DIM_LON_NAME))
	{
		mSedDB = sed_db;
	}
}

std::shared_ptr<darr::DimsArray<short>> ax::ocean::Dataset::getSedimentDB() const
{
	return mSedDB;
}

void ax::ocean::Dataset::setDepthDB(std::shared_ptr<darr::DimsArray<short>> depths_db)
{
	if (depths_db &&
		depths_db->hasDimension(DIM_LAT_NAME) &&
		depths_db->hasDimension(DIM_LON_NAME))
	{
		mDepthDB = depths_db;
	}
}

std::shared_ptr<darr::DimsArray<short>> ax::ocean::Dataset::getDepthDB() const
{
	return mDepthDB;
}

float ax::ocean::Dataset::getSsp(double lon, double lat, double depth, float default_val) const
{
	float result = default_val;

	if (mSspDB)
	{
		result = mSspDB->at({
				{ DIM_DEPTH_NAME, depth },
				{ DIM_LAT_NAME, lat },
				{ DIM_LON_NAME, lon } });
	}

	return result;
}

ax::ocean::Dataset::SspDepthValue ax::ocean::Dataset::getSsp(double lon, double lat) const
{
	SspDepthValue result;

	if (mSspDB)
	{
		auto depth_dim = mSspDB->dimensionScale(DIM_DEPTH_NAME);
		for (const auto& loop_depth : depth_dim)
		{
			float loop_ssp = mSspDB->at({
				{ DIM_DEPTH_NAME, loop_depth },
				{ DIM_LAT_NAME, lat },
				{ DIM_LON_NAME, lon } });

			result[loop_depth] = loop_ssp;
		}
	}

	return result;
}

short ax::ocean::Dataset::getSediment(double lon, double lat) const
{
	short result = 5;

	if (mSedDB)
	{
		result = mSedDB->at({
				{ DIM_LAT_NAME, lat },
				{ DIM_LON_NAME, lon } });
	}

	return result;
}

short ax::ocean::Dataset::getDepth(double lon, double lat, short default_depth) const
{
	short result = default_depth;

	if (mDepthDB)
	{
		result = mDepthDB->at({
				{ DIM_LAT_NAME, lat },
				{ DIM_LON_NAME, lon } });
	}

	return result;
}
